package com.itsq.service.resources.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itsq.common.bean.ErrorEnum;
import com.itsq.common.constant.APIException;
import com.itsq.mapper.CompanyMapper;
import com.itsq.mapper.DepartmentMapper;
import com.itsq.pojo.dto.PagePersonnelDto;
import com.itsq.pojo.dto.PersonnelDto;
import com.itsq.pojo.entity.Company;
import com.itsq.pojo.entity.Department;
import com.itsq.pojo.entity.Personnel;
import com.itsq.mapper.PersonnelMapper;
import com.itsq.service.resources.PersonnelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itsq.utils.GemBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 史先帅
 * @since 2021-01-30
 */
@Service
public class PersonnelServiceImpl extends ServiceImpl<PersonnelMapper, Personnel> implements PersonnelService {

    @Autowired(required = false)
    private PersonnelMapper personnelMapper;
    @Autowired(required = false)
    private CompanyMapper companyMapper;
    @Autowired(required = false)
    private DepartmentMapper departmentMapper;

    @Override
    public Page<Personnel> selPersonnelPageFront(PagePersonnelDto pagePersonnelDto) {
        Page<Personnel> page=new Page<>(pagePersonnelDto.getPageIndex(),pagePersonnelDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pagePersonnelDto.getOrderByField()==null){
            queryWrapper.orderByDesc("cre_date");
        }else{
            queryWrapper.orderByDesc(pagePersonnelDto.getOrderByField());
        }
        if(pagePersonnelDto.getName()!=null){
            queryWrapper.like("name",pagePersonnelDto.getName());
        }
        if(pagePersonnelDto.getPosition()!=null){
            queryWrapper.eq("position",pagePersonnelDto.getPosition());
        }
        if(pagePersonnelDto.getCompanyId()!=null){
            queryWrapper.eq("company_id",pagePersonnelDto.getCompanyId());
        }
        if(pagePersonnelDto.getDepartmentId()!=null){
            queryWrapper.eq("department_id",pagePersonnelDto.getDepartmentId());
        }
        if(pagePersonnelDto.getIsOk()!=null){
            queryWrapper.eq("isok",pagePersonnelDto.getIsOk());
        }
        return personnelMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Page<Personnel> selPersonnelPageAfter(PagePersonnelDto pagePersonnelDto) {
        Page<Personnel> page=new Page<>(pagePersonnelDto.getPageIndex(),pagePersonnelDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pagePersonnelDto.getOrderByField()==null){
            queryWrapper.orderByDesc("cre_date");
        }else{
            queryWrapper.orderByDesc(pagePersonnelDto.getOrderByField());
        }
        if(pagePersonnelDto.getName()!=null){
            queryWrapper.like("name",pagePersonnelDto.getName());
        }
        if(pagePersonnelDto.getPosition()!=null){
            queryWrapper.eq("position",pagePersonnelDto.getPosition());
        }
        if(pagePersonnelDto.getCompanyId()!=null){
            queryWrapper.eq("company_id",pagePersonnelDto.getCompanyId());
        }
        if(pagePersonnelDto.getDepartmentId()!=null){
            queryWrapper.eq("department_id",pagePersonnelDto.getDepartmentId());
        }
        if(pagePersonnelDto.getIsOk()!=null){
            queryWrapper.eq("isok",pagePersonnelDto.getIsOk());
        }
        return personnelMapper.selectPage(page,queryWrapper);
    }

    @Override
    public PersonnelDto selPersonnelByUserId(Integer userId) {
        return personnelMapper.selPersonnelByUserId(userId);
    }

    @Override
    public PersonnelDto insert(PersonnelDto personnel) {
        String userId = personnel.getUserId();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        Integer integer = personnelMapper.selectCount(queryWrapper);
        if(integer<1){
            QueryWrapper queryWrapper2=new QueryWrapper();
            queryWrapper2.eq("name",personnel.getStr1());
            Company company = companyMapper.selectOne(queryWrapper2);
            QueryWrapper queryWrapper3=new QueryWrapper();
            queryWrapper3.eq("name",personnel.getStr2());
            Department department = departmentMapper.selectOne(queryWrapper3);
            if(company!=null&&department!=null){
                personnel.setCompanyId(company.getId());
                personnel.setDepartmentId(department.getId());
                int i = personnelMapper.insert(GemBeanUtils.copyProperties(personnel, Personnel.class));
                if(i<1){
                    throw new APIException(ErrorEnum.is_ISNULL);
                }
                return personnel;
            }
        }else{
            QueryWrapper queryWrapper4=new QueryWrapper();
            queryWrapper4.eq("name",personnel.getStr1());
            Company company = companyMapper.selectOne(queryWrapper4);
            QueryWrapper queryWrapper5=new QueryWrapper();
            queryWrapper5.eq("name",personnel.getStr2());
            Department department = departmentMapper.selectOne(queryWrapper5);
            if(company!=null&&department!=null) {
                personnel.setCompanyId(company.getId());
                personnel.setDepartmentId(department.getId());
                Integer i = personnelMapper.updateById(GemBeanUtils.copyProperties(personnel, Personnel.class));
                if (i < 1) {
                    throw new APIException(ErrorEnum.is_UPDATE);
                }
                return personnel;
            }
        }
        return personnel;
    }

    @Override
    public Integer update(Personnel personnel) {
        String userId = personnel.getUserId();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        Integer integer = personnelMapper.selectCount(queryWrapper);
        if(integer>0){
            int i = personnelMapper.updateById(personnel);
            if(i<1){
                throw new APIException(ErrorEnum.is_UPDATE);
            }
            return i;
        }
        return 0;
    }

    @Override
    public Integer updateIsOk(Personnel personnel) {
        int i = personnelMapper.updateById(personnel);
        if(i<1){
            throw new APIException(ErrorEnum.is_UPDATE);
        }
        return i;
    }

    @Override
    public Integer delete(Integer id) {
        return personnelMapper.deleteById(id);
    }
}
